package com.vehicle.service.ServiceImpl;

import com.vehicle.mapper.BrandMapper;
import com.vehicle.mapper.CarMapper;
import com.vehicle.mapper.VehicleTypeMapper;
import com.vehicle.pojo.Brands;
import com.vehicle.pojo.Cars;
import com.vehicle.pojo.VehicleType;
import com.vehicle.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceimpl implements AdminService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private VehicleTypeMapper vehicleTypeMapper;

    /**
     * 添加车辆信息
     * @param cars
     * @return
     */
    @Override
    public void addCar(Cars cars) {
        //查看品牌，车型，没有就新加
        String brandKey = cars.getBrandKey();
        String key = brandMapper.selectBrandKey(brandKey);
        if(key ==null){
            Brands brands = new Brands();
            brands.setBrand(brandKey);
            brands.setNumber(1);
            brandMapper.add(brands);
        }
        String vehicleTypeKey = cars.getVehicleTypeKey();
        String vehicleTypeKey1= vehicleTypeMapper.selectVehicleTypeKey(vehicleTypeKey);
        if(vehicleTypeKey1 == null){
            VehicleType vehicleType = new VehicleType();
            vehicleType.setType(vehicleTypeKey);
            vehicleType.setNumber(1);
            vehicleTypeMapper.add(vehicleType);
        }
        //添加车辆，查看数据库中是否已存在，存在库存加一，不存在添加新车型，状态设为1，lastUseTime不设
        String carsName = cars.getName();
        Cars cars1 = carMapper.selectByName(carsName);
        Long inventory = carMapper.selectInventory(carsName);
        if(cars1 == null){
            cars.setStatus(1);
            cars.setLastUseTime(null);
            cars.setInventory(1L);
            carMapper.add(cars);
        }else{
            Cars cars2 = new Cars();
            cars2.setInventory(inventory + 1);
            cars2.setId(cars1.getId());
            carMapper.update(cars2);
        }


    }


    /**
     * 更新车辆信息
     * @param cars
     * @return
     */
    @Override
    public void updateCar(Cars cars) {
        carMapper.update(cars);
    }
}
